/**
 * http://code.google.com/p/gwt-spot
 * 
 * GWT-Spot -- Copyright 2008-2009
 *          -- by Dunkel, Ricardo
 *  
 */
package de.ricardodunkel.gwtspot.client.api.common;

/**
 * An utility class to provide easier number handling.
 * 
 * <p>
 * 	<em>Just extend some basic functionalities with existing 
 *  code and new functions.</em>
 * </p>
 * 
 * @author Dunkel, Ricardo
 * 
 * @version 0.1
 *
 */
public class NumberUtility {

	/** Define static whitespace separated list of numbers. */
	public static final String NUMBERS = "0 1 2 3 4 5 6 7 8 9";
	
	/**
	 * Calculating a percent value by required parameters.
	 * 
	 * <p>
	 * 	<em>Hint</em>:<br/>
	 *  May loose some correctness by rounding <em>native int</em> 's.
	 * </p>
	 * 
	 * @param hundert  A value representing original size, in fact 100 percent.
	 * @param percent  A percentage to calculate from its original, 
	 * 				   i.e. <em>70</em> for <em>70%</em>.
	 * 
	 * @return  A new calculated value representing a desired percentage of given input.
	 */
	public static int getPercentage(int hundert, int percent) {
		return (hundert*percent) / 100;
	}
	
	/**
	 * Calculating a percent value by string parameters.
	 * 
	 * <p>
	 *	Same as {@link #getPercentage(int, int)} with parsing parameters before. 
	 * </p>
	 * 
	 * @param hundert  A string value representing original size, in fact 100 percent.
	 * @param percent  A string percentage value to calculate from its original, 
	 * 				   i.e. <em>70</em> for <em>70%</em>.
	 * 
	 * @return  A new calculated value representing a desired percentage of given input.
	 */
	public static int getPercentage(String hundert, String percent) {
		return getPercentage( Integer.parseInt(hundert), Integer.parseInt(percent) );
	}
	
	/**
	 * Check if a string value does contain numeric signs only.
	 * 
	 * @param number  Incoming value to verify.
	 * 
	 * @return  <strong>True</strong> if every sign is a number,
	 * 			<strong>false</strong> otherwise.
	 */
	public static boolean isNumeric(String number) {
		boolean result = true;
		for( int i = 0; i < number.length(); i++ ) {
			if( NUMBERS.indexOf( number.charAt(i) ) < 0 ) {
				result = false;
			}
		}
		return result;
	}

}
